package banding.com.google.Zxing.scanview;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import banding.com.google.Zxing.exceptions.FileCanNotReadException;
import banding.com.google.Zxing.utils.BitmapUtil;

/**
 * @author Yue
 * @date 2017/8/3 19:04
 */
public class CoreBitmapTempMgr {

    /**
     * @param bitmap
     * @param appTempPath 应用的缓存相对路径
     */
    static String tempToSd(Bitmap bitmap, String appTempPath) {
            String fileAbsolutePath = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            StringBuilder fileSb = new StringBuilder();
            fileSb.append(Environment.getExternalStorageDirectory().toString());
            fileSb.append("/");
            fileSb.append(appTempPath);

            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-HH-mm-ss-SSS");
            String f = format.format(new Date());
            fileSb.append("/");
            fileSb.append(f+".png");

            fileAbsolutePath = fileSb.toString();
            boolean flag = BitmapUtil.saveBitmap(bitmap, CompressFormat.PNG,new File(fileAbsolutePath));

        } else {
//            Toast.makeText()//提示SD卡有问题
        }

        return fileAbsolutePath;
    }
    static Bitmap loadBitmap(String bitmapAbsolutePath) throws FileCanNotReadException {
         return BitmapUtil.loadBitmap(bitmapAbsolutePath);
    }
}
